﻿using Pb;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoundModel: MonoBehaviour {

    public List<AudioClip> audioClipBG;
    private AudioSource BGSource;
    private List<AudioSource> audioSourceList = new List<AudioSource>();
    public static float _BGVolume=0.6f;
    public static float EffectVolume = 0.6f;
    private bool isReady = true;
    EginUser user = EginUser.Instance;

    private static SoundModel _instance;   

    private void Awake()
    {
        _instance = this;
        if (user.musicControl >= 1) {
           Destroy( GameObject.Find("SoundManger"));
            Debug.Log("销毁背景音乐");
        }
        user.musicControl++;
        loadAudioClip();
    }


    void Start()
    {
        
        DontDestroyOnLoad(this);
        isReady = true;
        updateBGVolume();
    }
    private void Update()
    {
        if (BGSource != null) {
            if (_BGVolume == 0f)
            {
                BGSource.volume = _BGVolume;
            }
            else
            {
                BGSource.volume = _BGVolume;
                
            }
        }
        
    }
    public void playbg() {
        GameObject.Find("backMusic").GetComponent<AudioSource>().Play();
    }
    public void Pausebg()
    {
        GameObject.Find("backMusic").GetComponent<AudioSource>().Pause();
    }

    /// <summary>
    /// 播放特效背景音
    /// </summary>
    public static void PlayEffectBg(AudioClip clip)
    {
        int num = 0;
        if (_instance.audioSourceList.Count > 0 && num <= _instance.audioSourceList.Count)
        {
            for (int i = 0; i < _instance.audioSourceList.Count; i++)
            {
                if (_instance.audioSourceList[i].isPlaying)
                {
                    //该音源被占用 不处理
                    num++;
                }
                else
                {
                    //音源没有被占用
                    _instance.audioSourceList[i].clip = clip;
                    //_instance.audioSourceList[i].name = clip.name;
                    _instance.audioSourceList[i].playOnAwake = true;
                    _instance.audioSourceList[i].loop = false;
                    _instance.audioSourceList[i].Play();
                    num++;
                    break;
                }
            }
        }
        else
        {
            AudioSource audioSource = _instance.gameObject.AddComponent<AudioSource>();
            _instance.audioSourceList.Add(audioSource);
        }
    }

    private void loadAudioClip()
    {
        BGSource = create(gameObject, audioClipBG[0], false);
        BGSource.loop = true;        
     }
    public static AudioSource create(GameObject parent, AudioClip ac, bool autoPlay)
    {
        GameObject go = new GameObject();
        go.transform.parent = parent.transform;
        go.transform.localScale = Vector3.one;
        AudioSource audioSource = go.AddComponent<AudioSource>();
        audioSource.clip = ac;
        audioSource.name = ac.name;
        audioSource.playOnAwake = autoPlay;
        audioSource.loop = autoPlay;

        return audioSource;
        
    }
    public void updateBGVolume()
    {
        if (BGSource != null && isReady)
        {
            BGSource.volume = _BGVolume;
            if (_BGVolume == 0.0f)
            {
                BGSource.Pause();
            }
            else if (!BGSource.isPlaying)
            {
                BGSource.Play();
            }
        }
    }
    public static void PlaySound(AudioClip effectSound)
    {
        
        if (EffectVolume > 0)
        {
            NGUITools.PlaySound(effectSound, EffectVolume);
        }
    }

    public void SwitchBgMusic(int GameType) {

        BGSource.clip = audioClipBG[GameType];
        BGSource.name = audioClipBG[GameType].name;
        BGSource.playOnAwake = true;
        BGSource.loop = true;
        BGSource.Play();
        if (_BGVolume != 0) {
            BGSource.volume = _BGVolume;
        }
        //if (BGSource != null)
        //{

        //}

        //else {
        //    Debug.Log(audioClipBG[GameType].name);
        //    BGSource = create(gameObject, audioClipBG[GameType], false);
        //    BGSource.loop = true;
        //    BGSource.Play();
        //}

    }

    public static void PlayEffectBg(AudioSource audioSource, AudioClip ac, bool autoPlay)
    {
        audioSource.clip = ac;
        audioSource.name = ac.name;
        audioSource.playOnAwake = autoPlay;
        audioSource.loop = false;
    }
}
